import javafx.scene.control.TextFormatter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

public class MainMenu {
    static HashMap<String, DingDan> dingdans = new HashMap<>();

    //主菜单方法
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean change = true;
        while (change) {
            System.out.println("**************欢迎光临阳光旅游网**************");
            System.out.println("1.添加出行订单 2.查看我的出行订单 3.修改订单信息 4.删除出行人 5.查看行程 6.退出系统");
            System.out.print("请选择:(1-6)");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    //添加出行订单
                    addDing();
                    break;
                case 2:
                    //查看我的出行订单
                    showDing();
                    break;
                case 3:
                    //修改订单信息
                    try {
                        UpdateDing();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 4:
                    //删除出行人
                    delete();
                    break;
                case 5:
                    //查看行程
                    break;
                case 6:
                    //退出
                    change = false;
                    break;
            }
        }
    }

    //添加出行订单方法
    public static void addDing() {
        boolean choice = true;
        String change = "";
        String name;
        int age;
        int bed;
        DingDan ding = new DingDan();
        HashMap<String, Person> person = new HashMap<>();
        Scanner input = new Scanner(System.in);
        System.out.print("请输入出行日期(如:20170501):");
        String date = input.next();
        System.out.print("请输入联系人电话:");
        String number = input.next();
        System.out.println("************添加出行人信息************");
        while (choice) {
            System.out.print("1.成人 2.儿童(1.2米以下) 3.老人(65岁以上)请选择类别:");
            int j = input.nextInt();
            switch (j) {
                case 1:
                    System.out.print("请输入姓名:");
                    name = input.next();
                    System.out.print("请输入年龄:");
                    age = input.nextInt();
                    Person per = new Person(name, age);
                    person.put(per.getName(), per);
                    per.money();
                    System.out.print("是否继续?y/n");
                    change = input.next();
                    if (change.equals("n")) {
                        choice = false;
                    }
                    break;
                case 2:
                    System.out.print("请输入姓名:");
                    name = input.next();
                    System.out.print("请输入年龄:");
                    age = input.nextInt();
                    System.out.print("是否占床: 1.占用 2.不占用");
                    boolean vj = true;
                    int x = 0;
                    x = input.nextInt();
                    if (x != 1) {
                        vj = false;
                    }
                    Person child = new Child(name, age, vj);
                    person.put(child.getName(), child);
                    child.money();
                    System.out.print("是否继续?y/n");
                    change = input.next();
                    if (change.equals("n")) {
                        choice = false;
                    }
                    break;
                case 3:
                    System.out.print("请输入姓名:");
                    name = input.next();
                    System.out.print("请输入年龄:");
                    age = input.nextInt();
                    if (age < 65) {
                        System.out.println("对不起,老年人订单需为65岁以上");
                        break;
                    }
                    Person old = new oldMan(name, age);
                    person.put(old.getName(), old);
                    old.money();
                    System.out.print("是否继续?y/n");
                    change = input.next();
                    if (change.equals("n")) {
                        choice = false;
                    }
                    break;
            }
        }
        ding.setNumber(number);
        ding.setData(date);
        ding.setMap(person);
        dingdans.put(ding.getNumber(), ding);
    }

    //查看出行订单方法
    public static void showDing() {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您的手机号以查找您的订单:");
        String number = input.next();
        DingDan dingDan = dingdans.get(number);
        System.out.println(dingDan.getData());
        System.out.println("详细消息:");
        System.out.println("姓名\t年龄\t儿童是否占床\t金额");
        HashMap<String, Person> man = dingDan.getMap();
        Iterator<String> iterator = man.keySet().iterator();
        while (iterator.hasNext()) {
            String num = iterator.next();
            Person son = man.get(num);
            if (son instanceof Person) {
                son.money();
            } else if (son instanceof oldMan) {
                oldMan old = (oldMan) son;
                old.money();
            } else {
                Child child = (Child) son;
                child.money();
            }
        }
    }

    //修改订单方法
    public static void UpdateDing() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您的手机号以修改您的订单:");
        String number = input.next();
        DingDan dingDan = dingdans.get(number);
        System.out.println("请输入新的出行日期:");
        String date = input.next();
        System.out.println("请输入新的手机号:");
        String num = input.next();
        if (num.length() < 11 || num.length() > 11) {
            throw new Exception("手机号必须为11位数");
        }
        dingDan.setData(date);
        dingDan.setNumber(num);
        dingdans.remove(number);
        dingdans.put(dingDan.getNumber(), dingDan);
        System.out.println("修改成功,请自行前往查看");
    }

    //删除出行人方法
    public static void delete() {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您的手机号以管理您的订单出行人:");
        String number = input.next();
        DingDan dingDan = dingdans.get(number);
        HashMap<String, Person> map = dingDan.getMap();
        System.out.println(map.size());
        Iterator<String> iterator = map.keySet().iterator();
        System.out.println("请输入您要删除的出行人姓名:");
        String name = input.next();
        while (iterator.hasNext()) {
            String deName = iterator.next();
            if (name.equals(deName)) {
                map.remove(deName);
                dingDan.setMap(map);
                dingdans.put(number, dingDan);
                System.out.println("删除成功");
                break;
            }
        }
    }
}
